package com.ssm.job;

import com.hbase.ApplicationConfig;
import com.sitech.common.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.persistence.criteria.CriteriaBuilder;

import static com.hbase.ApplicationConfig.*;
import static com.hbase.utils.RandomUtil.testMathRandom10000;
import static com.hbase.utils.RandomUtil.testUrl;

public class MyJob extends QuartzJobBean{
    private static int count=1;

    protected static final Logger logger = Logger.getLogger(MyJob.class);

//    @Autowired
//    private ApplicationConfig applicationConfig;

    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        logger.info("Myjob开始执行了。。。。"+arg0.getTrigger().getKey().getName());
        ApplicationContext applicationContext=(ApplicationContext)arg0.getJobDetail().getJobDataMap()
                .get("applicationContext");
        logger.info("获取到的容器是："+(count++)+"|"+applicationContext);
        ApplicationConfig applicationConfig=new ApplicationConfig();
        if(!StringUtils.isEmptyOrNull(ApplicationConfig.requestUrl)){
            executeGet(ApplicationConfig.requestUrl);
        }
        if(!StringUtils.isEmptyOrNull(ApplicationConfig.requestUrl1)){
            executeGet(ApplicationConfig.requestUrl1);
        }
        if(!StringUtils.isEmptyOrNull(ApplicationConfig.requestUrl2)){
            executeGet(ApplicationConfig.requestUrl2);
        }
        if(!StringUtils.isEmptyOrNull(ApplicationConfig.requestUrl3)){
            executeGet(ApplicationConfig.requestUrl3);
        }
        if(!StringUtils.isEmptyOrNull(ApplicationConfig.requestUrl4)){
            executeGet(ApplicationConfig.requestUrl4);
        }
        if(!StringUtils.isEmptyOrNull(ApplicationConfig.requestUrl5)){
            executeGet(ApplicationConfig.requestUrl5);
        }

        int app1=Integer.parseInt(APP1);
        int app4=Integer.parseInt(APP4);
        int app2=Integer.parseInt(APP2);
        int app3=Integer.parseInt(APP3);
        int app5=Integer.parseInt(APP5);
        int rootApp=Integer.parseInt(ROOT_APP);

        int sum=app1+app4+app2+app3+app5;

        int i=testMathRandom10000();
        int j=i%sum;

        if(j<app1){
            logger.info(appUrl1);
            executeGet(appUrl1);
        }else if(app1<=j&&j<app1+app2){
            logger.info(appUrl2);
            executeGet(appUrl2);
        }else if(app1+app2<=j&&j<app1+app2+app3){
            logger.info(appUrl3);
            executeGet(appUrl3);
        }else if(j>app1+app2+app3&&j<app1+app2+app3+app4){
            logger.info(appUrl4);
            executeGet(appUrl4);
        }else{
            logger.info(appUrl5);
            executeGet(appUrl5);
        }


//        System.out.println(applicationConfig.getRequestUrl());
//        executeGet("http://172.21.11.199:17085/ssm/user/queryUser1.do?id=admin");
//        executeGet("http://172.21.11.199:17085/ssm/user/queryUser.do?id=admin");

//        executeGet("http://127.0.0.1:58080/ssm/test/testSave.do");
//        executeGet("http://127.0.0.1:8080/ssm/user/queryUser.do?id=admin");

    }

    public static void executeGet(String url)
    {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse res = client.execute(get);
            if (res.getStatusLine().getStatusCode() == 200) {

            }
//          HttpEntity localHttpEntity = res.getEntity();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally
        {
            client.getConnectionManager().shutdown();
        }
    }
}
